package com.xbongbong.workorder.service.impl;/*/*/

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.entity.WorkOrderFlowUserEntity;
import com.xbongbong.saas.help.workflow.WorkOrderHelp;
import com.xbongbong.saas.model.WorkOrderFlowUserModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.workorder.service.WorkOrderFlowUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 *
 * @author 魏荣杰
 * @date 2019/4/16 10:32
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderFlowUserService")
public class WorkOrderFlowUserServiceImpl implements WorkOrderFlowUserService {
    private static final Logger LOG = LoggerFactory.getLogger(WorkOrderFlowUserServiceImpl.class);

    @Resource
    private WorkOrderFlowUserModel workOrderFlowUserModel;
    @Resource
    private UserModel userModel;
    @Resource
    private WorkOrderHelp workOrderHelp;

    @Override
    public void createNewMain(Long workOrderId, String corpid, String userId) throws XbbException {
        workOrderHelp.createNewMain(workOrderId, corpid, userId);
    }

    @Override
    public WorkOrderFlowUserEntity getMainUser(Long workOrderId, String corpid) {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("workOrderId",workOrderId);
        param.put("isMain",1);
        param.put("del",0);
        List<WorkOrderFlowUserEntity> workOrderFlowUserEntityList = workOrderFlowUserModel.findEntitys(param);
        if (Objects.nonNull(workOrderFlowUserEntityList) && !workOrderFlowUserEntityList.isEmpty()) {
            return workOrderFlowUserEntityList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<WorkOrderFlowUserEntity> getTeamUserList(Long workOrderId, String corpid) {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("workOrderId",workOrderId);
        param.put("del",0);
        return workOrderFlowUserModel.findEntitys(param);
    }

    @Override
    public void changeTeamInFlow(String mainUserId, List<String> coUserIds, String corpid, Long workOrderId) throws XbbException {
        List<WorkOrderFlowUserEntity> workOrderFlowUserEntities = getTeamUserList(workOrderId, corpid);
        Set<String> mainUserIds = new HashSet<>();
        Set<String> preCoUserIds = new HashSet<>();
        for (WorkOrderFlowUserEntity entity : workOrderFlowUserEntities) {
            if (entity.getIsMain().equals(1)) {
                mainUserIds.add(entity.getUserId());
            } else {
                preCoUserIds.add(entity.getUserId());
            }
        }

        //去除协同人内有负责人的情况
        coUserIds.remove(mainUserId);

        List<String> delUserIds = new ArrayList<>(preCoUserIds);
        preCoUserIds.retainAll(coUserIds);
        //需要删除的
        delUserIds.removeAll(preCoUserIds);
        //需要新增的
        coUserIds.removeAll(preCoUserIds);
        List<WorkOrderFlowUserEntity> workOrderFlowUserAddList = new ArrayList<>();
        if (StringUtil.isEmpty(mainUserId)) {
            delUserIds.addAll(mainUserIds);
        }
        if (!StringUtil.isEmpty(mainUserId) && !mainUserIds.contains(mainUserId)) {
            //负责人变更，删除原有负责人，新增新负责人
            delUserIds.addAll(mainUserIds);
            UserEntity userEntity = userModel.getByKey(mainUserId, corpid);
            if (Objects.nonNull(userEntity)) {
                WorkOrderFlowUserEntity workOrderFlowUserEntity = new WorkOrderFlowUserEntity();
                workOrderFlowUserEntity.setCorpid(corpid);
                workOrderFlowUserEntity.setDel(0);
                workOrderFlowUserEntity.setUserAvatar(userEntity.getAvatar());
                workOrderFlowUserEntity.setUserId(userEntity.getUserId());
                workOrderFlowUserEntity.setUserName(userEntity.getName());
                workOrderFlowUserEntity.setDataId(workOrderId);
                workOrderFlowUserEntity.setIsMain(1);
                workOrderFlowUserAddList.add(workOrderFlowUserEntity);
            }
        }
        if (coUserIds.size() > 0) {
            List<UserEntity> userList = userModel.getByUserId(coUserIds, corpid);
            for (UserEntity entity : userList) {
                WorkOrderFlowUserEntity workOrderFlowUserEntity = new WorkOrderFlowUserEntity();
                workOrderFlowUserEntity.setCorpid(corpid);
                workOrderFlowUserEntity.setDel(0);
                workOrderFlowUserEntity.setUserAvatar(entity.getAvatar());
                workOrderFlowUserEntity.setUserId(entity.getUserId());
                workOrderFlowUserEntity.setUserName(entity.getName());
                workOrderFlowUserEntity.setDataId(workOrderId);
                workOrderFlowUserEntity.setIsMain(0);
                workOrderFlowUserAddList.add(workOrderFlowUserEntity);
            }
        }
        if (delUserIds.size() > 0) {
            workOrderFlowUserModel.batchDeleteByWorkOrderId(corpid, Collections.singletonList(workOrderId), delUserIds);
        }
        if (workOrderFlowUserAddList.size() > 0) {
            workOrderFlowUserModel.insertBatch(workOrderFlowUserAddList);
        }
    }

    @Override
    public void dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) throws XbbException {
        try {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("userId", userId);
            param.put("del", 0);
            List<WorkOrderFlowUserEntity> workOrderUserEntityList = workOrderFlowUserModel.findEntitys(param);
            for (WorkOrderFlowUserEntity entity : workOrderUserEntityList) {
                entity.setUserName(userName);
                entity.setUserAvatar(userAvatar);
                workOrderFlowUserModel.update(entity);
            }

        } catch (Exception e) {
            LOG.error("WorkOrderFlowUserServiceImpl.dataConsistencyUpdateUser 出错，corpid=" + corpid + " userId=" + userId + "  userName=" + userName + "  userAvatar=" + userAvatar, e);
        }
    }
}
